import request from '@/utils/request'
import { obj2query } from '@/utils/tools'

interface ILogin {
  username: string
  password: string
}
// 约定返回数据类型
interface ILoginRes {
  msg: string
  path: string
  data: { token: string }
}
export function reqUserLogin(data: ILogin) {
  // return request.post<ILoginRes>('/admin/login', data)
  return request<ILoginRes>({
    url: '/admin/login',
    method: 'post',
    data
  })
}

export function reqUserInfo() {
  return request({
    url: '/admin/getinfo',
    method: 'post'
  })
}

export function reqLogout() {
  return request({
    url: '/admin/logout',
    method: 'post'
  })
}

export function reqChangePWD(data: { oldpassword: string; password: string; repassword: string }) {
  return request({
    url: '/admin/updatepassword',
    method: 'post',
    data
  })
}

export function reqUserRefreshToken() {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      resolve({
        code: 200,
        content: {
          access_token: '123456',
          token_type: 'bearer',
          refresh_token: '123456',
          expires_in: 3600,
          user_id: '1'
        }
      })
    })
  })
}

// 管理员列表
export function reqManagerList(page: number, query: { limit?: number; keyword: string } = { limit: 10, keyword: '' }) {
  const r = obj2query(query)
  return request({
    url: `/admin/manager/${page}${r}`,
    method: 'get'
  })
}

// 修改管理员状态
export function reqUpdateManagerStatus(id: number, status: number) {
  return request({
    url: `/admin/manager/${id}/update_status`,
    method: 'post',
    data: { status }
  })
}
// 新增管理员
export function reqAddManager(data: any) {
  return request({
    url: '/admin/manager',
    method: 'post',
    data
  })
}
// 修改管理员
export function reqUpdateManager(id: number, data: any) {
  return request({
    url: `/admin/manager/${id}`,
    method: 'post',
    data
  })
}
// 删除管理员
export function reqDeleteManager(id: number | number[]) {
  if (Array.isArray(id)) {
    return new Promise((resolve, reject) => {
      setTimeout(() => {
        reject({
          code: 400,
          content: {
            msg: '参数id错误'
          }
        })
      }, 1000)
    })
  }
  return request({
    url: `/admin/manager/${id}/delete`,
    method: 'post'
  })
}
